package com.test.sparsearr01;

/**
 * @author: 蒿雨
 * @create: 2022-02-14 8:28
 * @description:稀疏数组棋盘问题
 */
public class SparseArray {
    public static void main(String[] args) {
        //创建稀疏数组10x10的格子
        int[][] arr = new int[10][10];
        arr[2][3] = 1;
        arr[3][4] = 2;

        //查看棋子在数组中的位置
        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.printf("%d\t", anInt);
            }
            System.out.println();
        }

        int sum = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (arr[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.printf("共有%d枚棋子\n", sum);
        //先行后列
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = 10;
        sparseArr[0][1] = 10;
        sparseArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (arr[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = arr[i][j];
                }
            }
        }
        System.out.println("行\t列\t值");
        for (int[] ints : sparseArr) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }

    }

}
